<template>
	<view>
		<view class="head">
			<img src="https://oa.yuxiang123.cn/assets/task/shop/login_bj.png" mode="widthFix" style="width: 100%;">
		</view>
		<view class="budd">
			<view class="wx_wc_1">
				<button v-if="is_new_user" class="wx_bt" open-type="getPhoneNumber" @getphonenumber="getphone">手机号快捷登录</button>
				<button v-else class="wx_bt"  @click="yzisCheck()">微信授权登录</button>
			</view>
			<view class="wx_wc"><view class="phone_bt" @click="toPages('/subone/public/phone_login?isCheck='+isCheck)">账号密码登录</view></view>
			<view style="display: flex;flex-wrap: wrap;color: #9E9E9E;margin-top: 20rpx;">
				<view style="width: 38%;text-decoration: underline;text-align: left;margin-left: 12%;" @click="toPages('/subone/public/forget_password')">忘记密码</view>
				<view style="width: 38%;text-decoration: underline;text-align: right;margin-right: 12%;" @click="toPages('/subone/public/register')">注册账号</view>
			</view>

			<view class="radio">
				<image src="@/static/radio.png" class="box" v-if="isCheck==false" @click="isCheck = true"></image>
				<image src="@/static/radio_2.png" class="box" v-else  @click="isCheck = false"></image>
				<view class="text">已阅读并同意《<text @click="toPages('agreement')">用户协议</text>》和《<text @click="toPages('privacy')">隐私政策</text>》</view>
			</view>
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				//动态赋值
				getCodeText: '获取验证码',
				getCodeisWaiting:false,
				phone:'',
				code:'',
				isCheck:false,
				is_new_user:true,
				mobile:''
			}
		},
		onLoad(p) {
			// #ifdef MP-WEIXIN
			var that = this;
			uni.login({
                success: function (res) {
                    console.log(res.code)
                    uni.request({
                        url:'https://oa.yuxiang123.cn/api/index/getOpenid',
                        method:'POST',
                        header:{"content-type":"application/x-www-form-urlencoded"},
                        data:{'code':res.code,'login':1,'is_shop':1},
                        success: (result) => {
							if(result.data.code == 1){ //老用户
								that.is_new_user = false;
								uni.setStorageSync('openid',result.data.data.openid);
								that.mobile = result.data.data.mobile;
							}else{
								uni.setStorageSync('openid',result.data.data);
							}
                        }
                    })	
                }					
            });
            // #endif
			uni.setStorageSync('num', 1);
		},
		methods: {
			yzisCheck(){
				if(this.isCheck==false){
					return this.$Toast('请阅读并同意《用户协议》以及《隐私政策》')
				}
				var invite_user_id=uni.getStorageSync('invite_user_id')?uni.getStorageSync('invite_user_id'):0;
				uni.request({
					url:'https://oa.yuxiang123.cn/api/index/xcxlogin',
					method:'POST',
					header:{"content-type":"application/x-www-form-urlencoded"},
					data:{'openid':uni.getStorageSync('openid'),'invite_user_id':invite_user_id,mobile:this.mobile,autocity:uni.getStorageSync('autocity'),is_shop:1},
					success: (res) => {
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						});
						if(res.data.code == 1){
							uni.setStorageSync('token', res.data.data.token);
							uni.setStorageSync('userInfo', res.data.data.user);
							
							setTimeout(() => {
								uni.navigateBack({
									delta:1
								})
							}, 800)
							
							// uni.switchTab({
							// 	url:'/pages/shop/shopClass'
							// })
						}else{
						}
					}
				})		
			},
			getphone(e){
				if(this.isCheck==false){
					return this.$Toast('请阅读并同意《用户协议》以及《隐私政策》')
				}
				if (!e.detail.iv) {
					uni.showToast({
						title:'获取手机号失败',
						icon:'none'
					})
					return;
				}
				uni.request({
					url:'https://oa.yuxiang123.cn/api/index/addEventStatistics',
					method:'POST',
					header:{"content-type":"application/x-www-form-urlencoded"},
					data:{'type':'xcx','user_id':uni.getStorageSync('openid'),'event':'手机号快捷登录'},
				})

				var that = this;
				var code = e.detail.code;
				var errMsg = e.detail.errMsg;
				
				console.log( e.detail);
				if(errMsg === 'getPhoneNumber:ok'){
					uni.request({ //获取用户手机号
						url: 'https://oa.yuxiang123.cn/api/index/getPhoneNumber?is_shop=1&code='+code,
						method: 'GET',
						success: function (res) {
							if(res.data.code == 1 ){
								var mobile = res.data.data;
								uni.login({
									success: function (res) {
										if(res.code) {
											let invite_user_id=uni.getStorageSync('invite_user_id')?uni.getStorageSync('invite_user_id'):0;
											uni.request({
												url:'https://oa.yuxiang123.cn/api/index/xcxlogin',
												method:'POST',
												header:{"content-type":"application/x-www-form-urlencoded"},
												data:{'code':res.code,'invite_user_id':invite_user_id,mobile:mobile,autocity:uni.getStorageSync('autocity'),is_shop:1},
												success: (res) => {
													uni.showToast({
														title:res.data.msg,
														icon:'none'
													});
													if(res.data.code == 1){
														uni.setStorageSync('token', res.data.data.token);
														uni.setStorageSync('userInfo', res.data.data.user);
														// uni.switchTab({
														// 	url:'/pages/user/user'
														// })
														
														setTimeout(() => {
															if(res.data.data.is_new == 1){
																uni.navigateTo({
																	url:'/subone/public/addUserInfo'
																})
															}else{
																if(uni.getStorageSync('task_user_id')){
																	uni.request({
																		url: 'https://oa.yuxiang123.cn/api/distribute_task/add_distribute_invite',
																		header:{
																			"content-type": "application/x-www-form-urlencoded"
																		},
																		data:{
																			token:uni.getStorageSync('token'),
																			task_user_id:uni.getStorageSync('task_user_id')
																		},
																		method: 'POST'
																	})
																	uni.setStorageSync('task_user_id','');
																	// uni.reLaunch({
																	// 	url:'/subone/user/distribute_task'
																	// })

																	uni.showModal({
																		title: '申请团长提交成功',
																		content: '你已成功提交团长申请!申请通过后，我们将通过邮件或短信通知你感谢你的支持和耐心等待!',
																		success: function (res) {
																			uni.switchTab({
																				url:'/pages/shop/shopClass'
																			})
																		}
																	});
																	
																}else{
																	uni.navigateBack({
																		delta:1
																	})
																}
															}
														}, 1000)
													}else{
													}
												}
											})						
										}else{
										}
									}					
								});

							}else{
								uni.showToast({
									title:'获取手机号失败,请重试!',
									icon:'none'
								})
								return;
							}
						}
					});
				}
			},
			//跳转
			toPages:function(url){
				uni.navigateTo({
					url:url
				})
			},
		}
	}
</script>
<style lang="scss">
.budd{
	border-radius: 40upx;
    margin-top: -20upx;
    background: #fff;
	
}
.radio{
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 40upx;
	.box{
		width:40upx;
		height:40upx;
	}
	.text{
		font-size: 26upx;
		color: #999999;
		margin-left: 10upx;
		text{
			color: #3662EC;
			text-decoration:underline;
		}
	}
}
.wx_wc_1{
	width: 100%;
	text-align: -webkit-center;
	padding-top: 120upx;
}
.wx_wc{
	width: 100%;
	text-align: -webkit-center;
	padding-top: 70upx;
}
.wx_bt{
	border-radius: 51px;
	background: #07C160;
	width: 630upx;
	height: 108upx;
	flex-shrink: 0;
	font-size: 34upx;
    line-height: 108upx;
	color: #fff;
	
}
.phone_bt{
	border-radius: 51px;
	background: #3662EC;
	color: #fff;
	width: 630upx;
	height: 108upx;
	flex-shrink: 0;
	font-size: 34upx;
    line-height: 108upx;
}
.head{
	width: 100%;
	//height:530upx;
	//background: url('https://oa.yuxiang123.cn/assets/task/shop/login_bj.png') ;
}

.bg{
	position: fixed;
	top: -200upx;
	bottom: -500upx;
	left: -300upx;
	right: -400upx;
	z-index: -1;
	image{
		width: 100%;
		height: 100%;
	}
}

.button{
	background-color: $uni-color;
	width: 90%;
	margin: auto;
	margin-top: 60upx;
	text-align: center;
	font-size: 30upx;
	line-height: 80upx;
	border-radius: 50upx;
	font-weight: bold;
}




</style>
